package com.xctech.ipb.dpn.common.vo;


import com.xctech.ipb.dpn.common.response.PageResponse;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

import static com.xctech.ipb.dpn.common.response.ResponseCode.SUCCESS;


@Getter
@Setter
public class MultiResult<T> extends Result<List<T>> {
    /**
     * 总记录数
     */
    private long total;
    /**
     * 当前页码
     */
    private long page;
    /**
     * 每页记录数
     */
    private long size;

    public MultiResult() {
        super();
    }

    public MultiResult(Boolean success, Integer code, String message, List<T> data, long total, long page, long size) {
        super(success, code, message, data);
        this.total = total;
        this.page = page;
        this.size = size;
    }

    public static <T> MultiResult<T> successMulti(List<T> data, long total, long page, long size) {
        return new MultiResult<>(true, SUCCESS.getCode(), SUCCESS.name(), data, total, page, size);
    }

    public static <T> MultiResult<T> errorMulti(Integer errorCode, String errorMsg) {
        return new MultiResult<>(true, errorCode, errorMsg, null, 0, 0, 0);
    }

    public static <T>  MultiResult<T> of(PageResponse<T> pageResponse) {
        if (pageResponse.getSuccess()) {
            return MultiResult.successMulti(pageResponse.getDatas(), pageResponse.getTotal(), pageResponse.getPageSize(), pageResponse.getCurrentPage());
        }
        return MultiResult.errorMulti(pageResponse.getResponseCode(), pageResponse.getResponseMessage());
    }

}
